package com.huanxing.cloud.mall.api.handler.impl;

import cn.dev33.satoken.secure.SaSecureUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.huanxing.cloud.common.core.constant.CacheConstants;
import com.huanxing.cloud.mall.api.service.IDistributionUserService;
import com.huanxing.cloud.mall.api.service.IUserInfoService;
import com.huanxing.cloud.mall.api.utils.HxTokenHolder;
import com.huanxing.cloud.mall.common.dto.HxTokenInfo;
import com.huanxing.cloud.mall.common.dto.UserLoginDTO;
import com.huanxing.cloud.mall.common.entity.UserInfo;
import com.huanxing.cloud.mall.common.enums.ClientTypeEnum;
import com.huanxing.cloud.mall.common.enums.MallErrorCodeEnum;
import com.huanxing.cloud.security.handler.HxBusinessException;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Component("PWD_LOGIN")
@AllArgsConstructor
public class UserPwdLoginHandler extends AbstractLoginHandler {

	private IUserInfoService userInfoService;

	private final RedisTemplate<String, String> redisTemplate;

	private final IDistributionUserService distributionUserService;

	@Override
	public void before(UserLoginDTO userLoginDTO) {

	}

	@Override
	public UserInfo doLogin(UserLoginDTO userLoginDTO) {

		final UserInfo userInfo = userInfoService.getByPhone(userLoginDTO.getPhone());
		if (ObjectUtil.isNull(userInfo)) {
			throw new HxBusinessException(MallErrorCodeEnum.ERROR_50001.getCode(),
					MallErrorCodeEnum.ERROR_50001.getMsg());
		}
		if (!userInfo.getPassword().equals(SaSecureUtil.md5(userLoginDTO.getPassword()))) {
			throw new HxBusinessException("密码错误");
		}
		final String clientType = userLoginDTO.getClientType();

		if (ClientTypeEnum.WX_MA.getCode().equals(clientType) || ClientTypeEnum.ALI_MA.getCode().equals(clientType)) {
			String hxToken = userLoginDTO.getHxToken();
			HxTokenInfo hxTokenInfo = HxTokenHolder.getTokenInfo();
			String tokenKey = CacheConstants.MALL_USER_TOKEN + hxToken;
			// 更新redis中的token信息
			hxTokenInfo.setMallUserId(userInfo.getId());
			redisTemplate.opsForValue()
				.set(tokenKey, JSONUtil.toJsonStr(hxTokenInfo), CacheConstants.TOKEN_TIME, TimeUnit.HOURS);
			userInfo.setHxToken(StrUtil.removeAll(tokenKey, CacheConstants.MALL_USER_TOKEN));
		}
		else {
			String token = UUID.randomUUID().toString();
			// 将token和用户信息存入redis，并设置过期时间
			String tokenKey = CacheConstants.MALL_USER_TOKEN + token;
			HxTokenInfo hxTokenInfo = new HxTokenInfo();
			hxTokenInfo.setClientType(clientType);
			hxTokenInfo.setMallUserId(userInfo.getId());
			hxTokenInfo.setTenantId(userInfo.getTenantId());
			redisTemplate.opsForValue()
				.set(tokenKey, JSONUtil.toJsonStr(hxTokenInfo), CacheConstants.TOKEN_TIME, TimeUnit.HOURS);
			userInfo.setHxToken(StrUtil.removeAll(tokenKey, CacheConstants.MALL_USER_TOKEN));
		}
		return userInfo;
	}

	@Override
	public void updateUser(UserInfo userInfo) {
		userInfoService.updateById(userInfo);
	}

	@Override
	public void distributionBindUser(UserInfo userInfo) {
		distributionUserService.bindUser(userInfo);
	}

}
